# Maintainer: LI Xuzhang <lixuzhang@gmail.com>

_realname=postgis
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.1.4
pkgrel=4
pkgdesc="Spatial and Geographic objects for PostgreSQL (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://postgis.net/"
license=('GPL2')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-geos"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-json-c"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-postgresql"
         #"${MINGW_PACKAGE_PREFIX}-protobuf-c"
         "${MINGW_PACKAGE_PREFIX}-proj"
         $([[ ${MSYSTEM} == MINGW* ]] && echo "${MINGW_PACKAGE_PREFIX}-gdal")
         )
options=('staticlibs' 'strip')
source=("https://download.osgeo.org/postgis/source/${_realname}-${pkgver}.tar.gz"
        pg_config)
sha256sums=('dc8e3fe8bc532e422f5d724c5a7c437f6555511716f6410d4d2db9762e1a3796'
            '3cd5a095cd4de7cb7d5f5fa9dbc02dfe02297ae1743c2961e70d9110d214f64f')

prepare() {
  cd ${_realname}-${pkgver}

  ./autogen.sh
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -r ${_realname}-${pkgver} "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --without-protobuf \
    $( [[ ${MSYSTEM} == MINGW* ]] || echo --without-raster ) \
    --with-pgconfig=${srcdir}/pg_config

  make
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  make DESTDIR="${pkgdir}" install
}
